package com.buka.goods.service.impl;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.buka.goods.mapper.ProductDetailsMapper;
import com.buka.goods.service.ProductDetailsService;
import com.buka.goods.service.ProductService;
import com.buka.server.pojo.Product;
import com.buka.server.pojo.ProductDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ProductDetailsServiceImpl implements ProductDetailsService {
    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Autowired
    private ProductService productService;
    @Override
    /**
     * 商品id查询详情表
     */
    public List<ProductDetails> findList(Long id) {
        ProductDetails productDetails = new ProductDetails();
        productDetails.setIsDelete(0);
        productDetails.setProductId(id);
        return productDetailsMapper.select(productDetails);
    }


    @Override
    @Transactional
    public Integer add(ProductDetails productDetails) {
        productDetails.setId(IdWorker.getId());
        Product product = this.min(productDetails);
        int i = 0;
        try {
            Integer integer = productService.update(product);
            i = productDetailsMapper.insertSelective(productDetails);
            if (integer != 1 ||  i!= 1) {
                throw new RuntimeException();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    @Override
    @Transactional
    public Integer update(ProductDetails productDetails) {
        productDetails.setUpdateTime(new Date());
        //
        Product product = new Product();
        product.setId(productDetails.getProductId());
        Product product1 = productService.findList(product).get(0);
        int i = 0;
        //获取最开始的商品详情
        ProductDetails old = productDetailsMapper.selectByPrimaryKey(productDetails.getId());
        //判断旧数据是不是最小价格
        if (product1.getPrice().compareTo(old.getPrice()) == 0){
            //判断旧数据价格是不是比新数据小
            if (old.getPrice().compareTo(productDetails.getPrice()) == -1) {
                //查询出商品关联的所有商品详细表
                List<ProductDetails> list = this.findList(productDetails.getProductId());
                //把新价格加到列表中
                for (ProductDetails p : list) {
                    if (productDetails.getId().compareTo(p.getId()) == 0){
                        p.setPrice(productDetails.getPrice());
                    }
                }
                //从关联的商品详情表中查询出最小价格
                BigDecimal min = list.get(0).getPrice();
                for (ProductDetails p : list) {
                    if (min.compareTo(p.getPrice()) == 1){
                        min = p.getPrice();
                    }
                }
                product.setPrice(min);
                try {
                    Integer integer = productService.update(product);
                    if (integer != 1) {
                        throw new RuntimeException();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                //新数据是最小价格，修改商品表
            }else if (old.getPrice().compareTo(productDetails.getPrice()) == 1){
                product.setPrice(productDetails.getPrice());
                try {
                    Integer integer = productService.update(product);
                    if (integer != 1) {
                        throw new RuntimeException();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            //判断新数据是不是比商品价格小
        }else {
            Product min = this.min(productDetails);
            try {
                Integer integer = productService.update(min);
                if (integer != 1) {
                    throw new RuntimeException();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        try {
            i = productDetailsMapper.updateByPrimaryKeySelective(productDetails);
            if ( i!= 1) {
                throw new RuntimeException();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    @Override
    @Transactional
    public Integer delete(Long id) {
        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(id);
        productDetails.setIsDelete(1);
        //查询商品id
        ProductDetails productDetails1 = productDetailsMapper.selectByPrimaryKey(id);
        //通过商品id查询商品对象
        Product products = new Product();
        products.setId(productDetails1.getProductId());
        Product product = productService.findList(products).get(0);
        //删除的对象价格和商品价格比较，如果商品价格等于删除对象的价格
        if (product.getPrice().compareTo(productDetails1.getPrice()) == 0){
            //查询出商品关联的所有商品详细表
            List<ProductDetails> list = this.findList(products.getId());
            //从关联的商品详情表中查询出最小价格
            BigDecimal min = list.get(0).getPrice();
            for (ProductDetails p : list) {
                if (id.compareTo(p.getId()) == 0){
                    continue;
                }
                if (min.compareTo(p.getPrice()) == 1){
                    min = p.getPrice();
                }
            }
            product.setPrice(min);
        }
        int i = 0;
        try {
            Integer integer = productService.update(product);
            i = productDetailsMapper.updateByPrimaryKeySelective(productDetails);
            if (integer != 1 ||  i!= 1) {
                throw new RuntimeException();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    @Override
    public boolean reducedInventory(Long id, Integer num) {
        int i = productDetailsMapper.reducedInventory(id,num);
        if (i == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean addInventory(Long id, Integer num) {
        int i = productDetailsMapper.addInventory(id, num);
        if (i == 1){
            return true;
        }
        return false;
    }

    public Product min(ProductDetails productDetails){
        Product product = new Product();
        product.setId(productDetails.getProductId());
        Product product1 = productService.findList(product).get(0);
        if (product1.getPrice() == null || product1.getPrice().compareTo(new BigDecimal(0)) == 0) {
            product1.setPrice(productDetails.getPrice());
        }else {
            if (product1.getPrice().compareTo(productDetails.getPrice()) == 1){
                product1.setPrice(productDetails.getPrice());
            }
        }
        return product1;
    }
}